package com.pengtu.gsj.service.impl.check.gcjs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.check.gcjs.CompletionAcceptanceDao;
import com.pengtu.gsj.dao.check.gcjs.StandardManagementDao;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.gcjs.CompletionAcceptance;
import com.pengtu.gsj.entity.gcjs.StandardManagement;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.gcjs.ComAcceptService;
import com.pengtu.gsj.service.check.gcjs.RuleManagerService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class ComAcceptServiceImpl implements ComAcceptService{

	@Autowired
	private AttachmentDao attachmentDao;
	@Autowired
	private CheckTableService checkTableService;	
	@Autowired
	private RuleManagerService ruleManagerService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private StandardManagementDao standardManagementDao;
	@Autowired
	private CompletionAcceptanceDao completionAcceptanceDao;

	@Override
	@Transactional(readOnly = true)
	public List<Attachment> getAttachmentList(QueryFilter fileter) {
		return attachmentDao.find(fileter);
	}

	@Override
	public List<CompletionAcceptance> getComAcceptProject() {
		return completionAcceptanceDao.getComAcceptProject();
	}
	
	@Override
	public Map<String, Object> getComAccepts(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<List<String>> totalScoreList = new ArrayList<List<String>>();
		List<StandardManagement> standardManagements = new ArrayList<StandardManagement>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double tableFull = Double.parseDouble((String)jsonObject.get("yearFull")); //表总分
		double total =0; //检查得分
		double percentage =0; //系统完善度
		
		//根据tag标签和时间、标签查询是否有附件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//根据startTime, endTime检索数据文件列表
		standardManagements = ruleManagerService.getCheckData(startTime, endTime);
		//单个项目的得分的每项检查项目的得分是
		double scoreEach = MathsUtils.divDouble(tableFull / 2, standardManagements.size(), 1);
		//遍历对象里面的附件对象，并检查里面是否有交工材料和竣工材料
		for (StandardManagement standardManagement : standardManagements) {
			//初始化List集合，分别用来保存检查项目一分数、检查项目二分数和当前检查分数
			List<String> checkResult = Arrays.asList("0", "0", "0");
			double temp = 0;
			for (Attachment attachment : standardManagement.getAttachments()) {
				if ("jgys_jgysbg".equals(attachment.getTag())){
					if ("0".equals(checkResult.get(0))) {
						temp += scoreEach;
					}
					checkResult.set(0, "1");
				}
				if ("jgys_jgysjds".equals(attachment.getTag())) {
					if ("0".equals(checkResult.get(1))) {
						temp += scoreEach;
					}
					checkResult.set(1, "1");
				}
			}
			checkResult.set(2, String.valueOf(temp));
			totalScoreList.add(checkResult);
			total = MathsUtils.formatDouble(total + temp, 1);
		}
		percentage = MathsUtils.divDouble(total * 100, tableFull, 2);
		//传递一个值，使每次都更新数据
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("percent", percentage);
		map.put("totalScoreList", totalScoreList);
		map.put("manageData", standardManagements);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}
